package com.hzw.saas.web.shell.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  在收到新的STOMP SUBSCRIBE时发布。
 * </p>
 *
 * @author sonam
 * @since 2021/5/21 16:15
 */
@Slf4j
@Component
public class SessionSubscribeListener implements ApplicationListener<SessionSubscribeEvent> {

    @Override
    public void onApplicationEvent(SessionSubscribeEvent event) {
        Message<byte[]> message = event.getMessage();
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
        StompCommand command = accessor.getCommand();
        if (command.equals(StompCommand.SUBSCRIBE)) {
            String sessionId = accessor.getSessionId();
            String subscriptionId = accessor.getSubscriptionId();
            String destination = accessor.getDestination();
            log.debug("SessionSubscribe, sessionId:{},subscriptionId:{},destination:{}",
                sessionId, subscriptionId, destination );
        }
    }

}
